import os

# import fastapi
from fastapi import APIRouter,HTTPException,Request
from application.utils import func

app = APIRouter()

@app.get('/api')
async def api()->dict:
    """
    测试接口
    :return:
    """
    return {'title':'FastApp'}


@app.get('/api_test')
async def api_test(name: str) -> dict:
    """测试异常接口"""
    try:
        print(username)
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

    return {'title': 'FastApp'}

@app.get('/sms/{mobile}')
async def sms(request:Request,mobile:str) -> dict:
    """
    发送验证码
    :param request:本次HTTP请求对象
    :param mobile: 要接受验证码的手机
    :return:
    """
    redis = request.app.state.redis
    code = func.gen_int(5)
    timer = 10 * 60
    ret = await redis.setex(f"sms_{mobile}",timer,code)
    if ret:
        # 发送短信
        print("成功保存数据到redis中了")
    return {'mobile':mobile,'sms_code':code}